package rd.system.mp3 {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author cuong.tran@rdsystem.org
	 */
	public class MP3 extends Sprite {
		
		private var strURL:String;
		private var sound:Sound;
		private var soundChannel:SoundChannel;
		private var isPlay:Boolean = false;
		private var isMove:Boolean = false;
		private var volume:Number = 1;
		private var curPosition:Number;
				
		public function MP3() {
			
			mcPlay.buttonMode = true;
			mcPlay.mouseChildren = false;
			mcPlay.addEventListener(MouseEvent.CLICK, onClickPlayHandler);
			
			mcPause.buttonMode = true;
			mcPause.mouseChildren = false;
			mcPause.addEventListener(MouseEvent.CLICK, onClickPauseHandler);	
			
			mcPlayed.mouseEnabled = false;
			
			mcBar.buttonMode = true;
			mcBar.mouseChildren = false;
			mcBar.addEventListener(MouseEvent.CLICK, onClickBarHandler);
			
			mcCursor.buttonMode = true;
			mcCursor.mouseChildren = false;
			mcCursor.addEventListener(MouseEvent.MOUSE_DOWN, onDownCursorHandler);
			
			mcVolume.buttonMode = true;
			mcVolume.mouseChildren = false;
			mcVolume.addEventListener(MouseEvent.CLICK, onClickVolumeHandler);
			
			addEventListener(Event.ENTER_FRAME, onEnterFramdHandler);
		}	
		
		private function onClickVolumeHandler(evt:MouseEvent):void {
			
			if (soundChannel) {
				mcVolume.gotoAndStop((mcVolume.currentFrame == 1) ? 2 : 1);				
				
				volume = (mcVolume.currentFrame == 1) ? 1 : 0;
				soundChannel.soundTransform = new SoundTransform(volume);
			}
		}
		
		private function onDownCursorHandler(evt:MouseEvent):void {
			isMove = true;
			mcCursor.startDrag(false, new Rectangle(mcBar.x, 33, mcBar.width - mcCursor.width, 0));
			
			if (isPlay) {
				soundChannel.stop();
			}
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveCursorHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, onUpCursorHandler);
		}
		
		private function onMoveCursorHandler(e:MouseEvent):void {
			var cursor:Number = mcCursor.x - mcBar.x;
			mcPlayed.width = cursor + mcCursor.width / 2;
			
			curPosition = Math.floor(sound.length * (mcCursor.x - mcBar.x) / (mcBar.width - mcCursor.width));
		}
		
		private function onUpCursorHandler(evt:MouseEvent):void {
			isMove = false;
			mcCursor.stopDrag();
			
			if (isPlay) {
				soundChannel = sound.play(curPosition);
				soundChannel.soundTransform = new SoundTransform(volume);
			}
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveCursorHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onUpCursorHandler);
		}
		
		private function onClickBarHandler(evt:MouseEvent):void {
			
			if (!sound) {
				return;
			}
			
			curPosition = Math.floor(sound.length * mcBar.mouseX / mcBar.width);
			
			if (isPlay) {
				soundChannel.stop();				
				soundChannel = sound.play(curPosition);
				soundChannel.soundTransform = new SoundTransform(volume);
				
			} else {
				
				var cursor:Number = Math.floor(curPosition / sound.length * (mcBar.width - mcCursor.width));
				mcCursor.x = mcBar.x + cursor;
				
				mcPlayed.width = cursor + mcCursor.width / 2;
			}
		}
		
		private function onClickPauseHandler(evt:MouseEvent):void {
			if (soundChannel && isPlay) {
				isPlay = false;
				
				curPosition = soundChannel.position;			
				soundChannel.stop();
				
				mcPlay.visible = true;
				mcPause.visible = false;
			}
		}
		
		private function onClickPlayHandler(evt:MouseEvent):void {			
			if (sound && isPlay == false) {
				isPlay = true;
				
				soundChannel = sound.play(curPosition);
				soundChannel.soundTransform = new SoundTransform(volume);
								
				mcPlay.visible = false;
				mcPause.visible = true;
			}
		}
		
		public function playMP3(pUrl:String):void {			
			
			if (soundChannel) {
				soundChannel.stop();
			}
			
			strURL = pUrl;
			volume = 1;
			
			sound = new Sound(new URLRequest(strURL));
			soundChannel = sound.play();
			soundChannel.soundTransform = new SoundTransform(volume);
			
			mcPlay.visible = false;
			mcPause.visible = true;
			mcVolume.gotoAndStop(1);
			
			isPlay = true;
		}
		
		private function onEnterFramdHandler(e:Event):void {
			
			if (isPlay == false || isMove == true) {
				return;
			}			
			
			if (sound && sound.length) {
				txtLength.text = formatTime(sound.length / 1000);
			}
			
			if (soundChannel) {
				txtPosition.text = formatTime(soundChannel.position / 1000);						
			}
			
			var cursor:Number = Math.floor(soundChannel.position / sound.length * (mcBar.width - mcCursor.width));
			mcCursor.x = mcBar.x + cursor;
			
			mcPlayed.width = cursor + mcCursor.width / 2;
			
			var position:Number = Math.floor(soundChannel.position / 1000);
			var length:Number = Math.floor(sound.length / 1000);
			
			if (position >= length && length != 0) {
				isPlay = false;
				curPosition = 0;
				
				txtPosition.text = formatTime(0);
				
				mcCursor.x = mcBar.x;
				mcPlayed.width = mcCursor.width / 2;
				
				mcPlay.visible = true;
				mcPause.visible = false;
			}
		}
		
		private function formatTime(time:int) :String {
			
			var hour:int = Math.floor(time / 3600);
			var strHour:String = (hour > 9) ? ('' + hour)  : ('0' + hour);
			
			time = time % 3600;
			var minute:int = Math.floor(time / 60);
			var strMinute:String = (minute > 9) ? ('' + minute)  : ('0' + minute);
			
			time = time % 60;
			var second:int = time % 60;
			var strSecond:String = (second > 9) ? ('' + second)  : ('0' + second);
			
			if (hour > 0) {			
				return strHour + ':' + strMinute + ':' + strSecond;
			}
			
			return strMinute + ':' + strSecond;
		}		
		
		override public function get width():Number {
			return 280;
		}
		
		override public function get height():Number {
			return 50;
		}
	}
}